
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.BasePayRequestParams;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:短信支付接口
 *
 * @author wcj
 * @since 2015年11月16日 上午10:21:24
 */
public class SMSPayTask {

    public SMSPayHttpResponse request(BasePayRequestParams payParams) {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        // 填充参数，key-value。key是接口要求传的变量名称
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_RECHARGE);
        params.put("username", UserManager.getInstance().getCurrentUserInternal().getAccountName());
        params.put("outorderid", payParams.getOutOrderId());
        params.put("directcharge", payParams.getDirectCharge());
        params.put("money", payParams.getMoney());
        params.put("chargetype", payParams.getChargeType());
        params.put("alone", 0);
        params.put("role", payParams.getRole());
        if (!TextUtils.isEmpty(payParams.getExtension1())) {
            params.put("extension1", payParams.getExtension1());
        }
        if (!TextUtils.isEmpty(payParams.getExtension2())) {
            params.put("extension2", payParams.getExtension2());
        }
        requestParams.add(params);

        // 生成请求包
        HttpBaseRequestPackage requestPackage = new HttpBaseRequestPackage();
        requestPackage.setUserName(payParams.getUserName());
        // 生成响应包
        ResponsePackage responsePackage = new HttpResponsePackage();
        // 生成响应类
        SMSPayHttpResponse appsHttpResponse = new SMSPayHttpResponse();

        requestPackage.setRequestParams(requestParams);

        try {
            KGHttpClient.request(requestPackage, responsePackage);
            // 响应类赋值
            responsePackage.getResponseData(appsHttpResponse);
            // 返回响应类
            return appsHttpResponse;
        } catch (AppException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 响应包
    private class HttpResponsePackage extends HttpBaseResponsePackage<SMSPayHttpResponse> {

        @Override
        protected void handleResponse(SMSPayHttpResponse response, String jsonStr) {
            try {
                LogUtil.d("response", "SMSPayTask:" + jsonStr);
                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (response.isUseful()) {
                        JSONObject jsonResult = jsonResp.getJSONObject("data");
                        response.setResult_code(jsonResult.getString("code"));
                        response.setResult_msg(jsonResult.getString("msg"));
                        response.setResult_orderid(jsonResult.getString("orderid"));
                        response.setResult_url(jsonResult.optString("url"));
                        response.setResult_params(jsonResult.optString("param"));
                        response.setResult_desc(jsonResult.optString("desc"));
                    }
                }
                response.setOk(true);
            } catch (Exception e) {
            }
        }
    }

    public class SMSPayHttpResponse extends ResponseMessage {

        /**
         * 
         */
        private static final long serialVersionUID = 317187261252464570L;

        private String result_code;

        private String result_msg;

        private String result_orderid;

        private String result_url;

        private String result_params;

        private String result_desc;

        public String getResult_code() {
            return result_code;
        }

        public void setResult_code(String result_code) {
            this.result_code = result_code;
        }

        public String getResult_msg() {
            return result_msg;
        }

        public void setResult_msg(String result_msg) {
            this.result_msg = result_msg;
        }

        public String getResult_orderid() {
            return result_orderid;
        }

        public void setResult_orderid(String result_orderid) {
            this.result_orderid = result_orderid;
        }

        public String getResult_url() {
            return result_url;
        }

        public void setResult_url(String result_url) {
            this.result_url = result_url;
        }

        public String getResult_params() {
            return result_params;
        }

        public void setResult_params(String result_params) {
            this.result_params = result_params;
        }

        public String getResult_desc() {
            return result_desc;
        }

        public void setResult_desc(String result_desc) {
            this.result_desc = result_desc;
        }

    }
}
